package cn.hn.tx.rebot.blog.service.impl;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import cn.hn.comm.LocalFiles;
import cn.hn.comm.Tools;
import cn.hn.comm.exception.IExceptionService;
import cn.hn.comm.ibatis.BaseDAO;
import cn.hn.net.Kit;
import cn.hn.tx.rebot.blog.model.Blog;
import cn.hn.tx.rebot.blog.model.Comment;
import cn.hn.tx.rebot.blog.service.ICrawlBlogService;
import cn.hn.tx.rebot.blog.service.IRebotBlogService;

/**
 * 抓取服务接口实现
 * 
 * @author shenhao
 * 
 */
public class CrawlServiceImpl_SinaEladies implements ICrawlBlogService {

	// 日志前缀
	private String url_blogPrefix = "http://club.eladies.sina.com.cn/";

	// DAO基类
	private BaseDAO baseDAO;

	private IRebotBlogService rebotBlogService;
	
	//异常记录
	private IExceptionService exceptionService;
	
	private int commentNum;
	
	
	public List<Comment> getComments(String url)
	{
		System.out.println("抓取评论：" + url);

		List<Comment> listComment=new ArrayList<Comment>();
		try {
			// 得到评论HTML
			String html = Kit.httpRequest(url, null, "GET", "gb2312");
//			LocalFiles lf = new LocalFiles();
//			String html=lf.readTxtFile("D://comment.txt");		
			if (html != null) {
				//得到评论 "<a.href=\"(.{1,30}html)\".*?target=.*?>");
				List<String> htmlComments = Tools.getHtmlNodes(html,
						"<div class=\"cont f14\" id=.{1,30}>(.*?)</div>");
				
				if(htmlComments.size()>1)
				{
					for(int i=1;i<htmlComments.size();i++)
					{
						Comment comment=new Comment();
						comment.setComment(htmlComments.get(i).trim());
						listComment.add(comment);
					}
				}
			}
		} catch (Exception e) {
//			e.printStackTrace();
			exceptionService.insertException(this.getClass().getName(), "getComment()", e.toString());
		}		
		
		return listComment;
	}
	
	/**
	 * 得到日志
	 */
	public Blog getBlog(String url) {

		System.out.println("抓取日志：" + url);


		try {
//			Kit kit = new Kit();
			// 得到日志HTML
			String html = Kit.httpRequest(url, null, "GET", "gb2312");
			if (html != null) {
				// 得到标题
				List<String> htmlTitle = Tools.getHtmlNodes(html,
						"<span style=\"font-size:14px;.*?>(.*?)</span>");
				String orgTitle = htmlTitle.get(0).trim();

				// 数据库中找一下有没有一样的
				List listTitle = rebotBlogService
						.queryListRebotWriteBlog(orgTitle);
				if (listTitle.size() == 0) {
					// 得到正文
					List<String> htmlContent = Tools.getHtmlNodes(html,
							"<div class=.cont f14.*?>(.*?)</div>");
					String content = htmlContent.get(0).trim();

					Blog blog = new Blog();
					blog.setUrl(url);
					blog.setTitle(orgTitle);
					blog.setContent(content);

					return blog;
				} else {
					return null;
				}

			} else {
				return null;
			}
		} catch (Exception e) {
//			e.printStackTrace();
			exceptionService.insertException(this.getClass().getName(), "getBlog()", e.toString());
			return null;
		}
	}

	/**
	 * 得到日志
	 */
	public List<Blog> getBlogs(String url) {

		// 日志队列
		List<Blog> listBlog = new ArrayList<Blog>();

		List<String> listBlogUrl = new ArrayList<String>();

		String html = null;

		LocalFiles lf = new LocalFiles();

		// LocalFiles lf = new LocalFiles();

		// kit.httpRequest("http://61.135.169.105", null, "GET","gb2312");

		// http://club.eladies.sina.com.cn/forum-25-75.html
		// String response = lf.readTxtFile("D:\\html.txt");
		// List<String> listMe = Tools.getHtmlNodes(response,
		// "<a.href=\"(.{1,30}html)\".*?target=.*?>"
		// // "<\"(html)\">"
		// );
		//		
		// System.out.println(listMe.get(0));
		// if(true)
		// {
		// return null;
		// }

		html = Kit.httpRequest(url, null, "GET", "gb2312");
		if (html != null) {

			// try {
			// lf.writeTxtFile("D:"+File.separator+"html.txt", response);
			// } catch (IOException e) {
			// // TODO Auto-generated catch block
			// e.printStackTrace();
			// }

			// System.out.println(response);

			// response="<tbody id=>abc</tbody>";

			// <p.*?>(.*?)</p> <tbody*>([^</tbody>]*)
			// "<div.+?id=.*?tree_.+?>(.*)</div>"
			// 得到所有日志节点
			List<String> htmlNodes = Tools.getHtmlNodes(html,
					"<tbody.*?>(.*?)</tbody>");

			// 得到所有日志节点的超链接地址
			for (String node : htmlNodes) {
				// 标题 <a href="thread-180148-1-1.html"
				// target="_blank">我的爱该怎么继续??</a>
				// List<String> htmlTitleNodes = Tools.getHtmlNodes(node,
				// "<a.href=\".*?\".*?target=.*?>(.*?)</a>");
				// String curTitle = htmlTitleNodes.get(0);
				// // 数据库中找一下有没有一样的
				// List listTitle = rebotBlogService
				// .queryListRebotWriteBlog(curTitle);
				// System.out.println("标题=" + listTitle);
				// if (listTitle.size() == 0) {
				// 链接地址
				List<String> htmlANodes = Tools.getHtmlNodes(node,
						"<a.href=\"(.{1,30}html)\".*?target=.*?>");
				listBlogUrl.add(url_blogPrefix + htmlANodes.get(0));
				// }
			}

			for (String blogUrl : listBlogUrl) {
				Blog blog = getBlog(blogUrl);
				if (blog != null) {
					listBlog.add(blog);
				}
			}

			// Blog blog = getBlog(listBlogUrl.get(0));
			// listBlog.add(blog);

			// try {
			// Element root = null;
			// root = DocumentHelper.parseText(response).getRootElement();
			// List<Element> listElement=new ArrayList<Element>();
			// kit.getElements(root, listElement, "tbody");
			// System.out.println(listElement);
			// } catch (Exception e) {
			// e.printStackTrace();
			// }

			return listBlog;
		} else {
			return null;
		}
	}

	public BaseDAO getBaseDAO() {
		return baseDAO;
	}

	public void setBaseDAO(BaseDAO baseDAO) {
		this.baseDAO = baseDAO;
	}

	public String getUrl_blogPrefix() {
		return url_blogPrefix;
	}

	public void setUrl_blogPrefix(String url_blogPrefix) {
		this.url_blogPrefix = url_blogPrefix;
	}

	public IRebotBlogService getRebotBlogService() {
		return rebotBlogService;
	}

	public void setRebotBlogService(IRebotBlogService rebotBlogService) {
		this.rebotBlogService = rebotBlogService;
	}

	
	
	public IExceptionService getExceptionService() {
		return exceptionService;
	}

	public void setExceptionService(IExceptionService exceptionService) {
		this.exceptionService = exceptionService;
	}
	
	

	public int getCommentNum() {
		return commentNum;
	}

	public void setCommentNum(int commentNum) {
		this.commentNum = commentNum;
	}

	public static void main(String[] args) throws UnsupportedEncodingException {
		// String t = "测试";
		// String a = new String(t.getBytes("GBK"), "GBK");
		// System.out.println(a);

		CrawlServiceImpl_SinaEladies cs = new CrawlServiceImpl_SinaEladies();
		List<Comment> listComment= cs.getComments(null);
		for(Comment c:listComment)
		{
			System.out.println(c.getComment());
			System.out.println("------------------------------------------------");
		}
		// cs.getBlogs(null);
//		Blog blog = cs
//				.getBlog("http://club.eladies.sina.com.cn/thread-4271949-1-1.html");

	}
}
